package com.ingsha.prometheus.config;

import com.ingsha.prometheus.entity.BrokerDto;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PrometheusWatchObj
 * @Description TODO
 * @Author Martin Yi
 * @Date 2023/12/10 19:49
 * @Version 1.0
 */
@Component
@RequiredArgsConstructor
public class PrometheusStorage {

    public Map<String, BrokerDto> brokerMap = new HashMap<>();

    private final MeterRegistry meterRegistry;

    public void updateDto(BrokerDto dto) {
        //不存在则添加进map,并创建gauge
        if (!brokerMap.containsKey(dto.getBroker())) {
            brokerMap.put(dto.getBroker(), dto);
            Gauge
                    .builder("user_service", dto, j -> j.getUser())
                    .tag("broker", dto
                            .getBroker())
                    .register(meterRegistry);
        }
        //存在后,修改对应的值即可
        BrokerDto oldBroker = brokerMap.get(dto.getBroker());
        oldBroker.setUser(dto.getUser());
        oldBroker.setBroker(dto.getBroker());
    }

}
